package com.design.proxy.cglibproxy;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * cglib动态代理
 *
 * @author: cong zhi
 * @createDate: 2022/5/14 16:47
 * @updateUser: cong zhi
 * @updateDate: 2022/5/14 16:47
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public class CglibMeipo implements MethodInterceptor {


    public Object getInstance(Class<?> clazz) {
        // 相当于Proxy，代理工具类
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object invoke = methodProxy.invokeSuper(o, objects);
        after();
        return invoke;
    }

    private void after() {
        System.out.println("双方同意：开始交往");
    }

    private void before() {
        System.out.println("我是媒婆，已经收集到你的需求，开始物色");
    }
}

